import util.StringUtil;

/**
 * Created by gamy on 2017/7/17.
 */
public class CreateResponse {

    public void create() {
        //user
        createResponse("User", false, "user");
        createResponse("User", true, "userList");

        //feed
        createResponse("Feed", false, "feed");
        createResponse("Feed", true, "feedList");

        //baby
        createResponse("Baby", false, "baby");
        createResponse("Baby", true, "babyList");
    }

    private void createResponse(String dao, boolean isList, String name) {
        String upperName = StringUtil.makeStringLowerOrUpper(name, 0, true);

        String s = "";
        s += "package com.st.response;\n\n";
        s += "import com.st.dao.base.STError;\n";
        s += "import com.st.dao." + dao.toLowerCase() + "." + dao + "" + ";\n";
        if (isList) {
            s += "import java.util.ArrayList;\n";
        }

        s += "\n";


        s += "/**\n" +
                " * Created by gamy on 2017/7/16.\n" +
                " */\n\n";

        String className = upperName + "Response";
        s += "public class " + className + " extends STResponse {\n\n";
        String clazz = dao;
        if (!isList) {
            clazz = dao;
        } else {
            clazz = "ArrayList<" + dao + "> ";
        }
        s += "\tprivate " + clazz + " " + name + ";\n\n";

        s += "    public " + className + "() {\n" +
                "        super();\n" +
                "    }\n" +
                "\n" +
                "    public " + className + "(STError error){\n " +
                "        super(error);\n" +
                "    }\n\n";

        s += getGetterMethod(clazz, name);
        s += getSetterMethod(clazz, name);

        s += "\n}";
        System.out.println(s);

        writeString(s, className);
    }

    private void writeString(String s, String className) {
        String file = "/Users/gamy/projects/java/HeyJersey/src/com/st/response/" + className + ".java";
        StringUtil.writeFile(s, file);
    }


    private String getGetterMethod(String dao, String name) {
        String s = "";
        String upperName = StringUtil.makeStringLowerOrUpper(name, 0, true);
        s += "\tpublic " + dao + " get" + upperName + "() {\n" +
                "\t\treturn " + name + ";\n" +
                "\t}\n";

        return s;
    }

    private String getSetterMethod(String dao, String name) {
        String s = "";
        String upperName = StringUtil.makeStringLowerOrUpper(name, 0, true);
        s += "\tpublic void set" + upperName + "(" + dao + " " + name + ") {\n" +
                "\t\tthis." + name + " = " + name + ";\n" +
                "\t}\n";

        return s;
    }
}
